import { login, UserAPI, UserToAPI } from '@/api/user'
import { resetRouter } from '@/router'
import { getToken, setToken as setTokencokie, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 存储token
    setToken(state, newToken) {
      state.token = newToken
      setTokencokie(newToken)
    },
    // 存储用户信息
    setuserInfo(state, user) {
      state.userInfo = user
    },
    // 退出登录，进行修改操作
    logOut(state) {
      state.token = ''
      state.userInfo = {}
      removeToken()
      resetRouter()
      state.commit('permission/filterRoutes', [])
    }
  },
  actions: {
    async getToken(store, dataToken) {
      const res = await login(dataToken)
      console.log(res)
      store.commit('setToken', res.data)
      return res
    },
    async getUser(store) {
      const res = await UserAPI()
      const res1 = await UserToAPI(res.data.userId)
      // console.log(res)
      // console.log(res1)
      const res3 = {
        ...res.data,
        ...res1.data
      }
      store.commit('setuserInfo', res3)
      return res3
    }
  },
  getters: {}
}
